package com.svtar.ywg.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Svtar on 2019/4/2.
 * 时间工具
 */

public class TimeUtils {

    public static String getStringByFormat(long paramLong, String paramString) {
        String date;
        try {
            SimpleDateFormat format = new SimpleDateFormat(paramString);
            date = format.format(Long.valueOf(paramLong));
        } catch (Exception e) {
            e.printStackTrace();
            date = null;
        }
        return date;
    }

    public static String getStringByFormat(long paramLong) {
        return getStringByFormat(paramLong, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getStringByFormat() {
        return getStringByFormat(new Date().getTime());
    }

    /**
     * 时间转换为时间戳
     *
     * @param timeStr 时间 例如: 2016-03-09
     * @param format  时间对应格式  例如: yyyy-MM-dd
     * @return
     */
    public static long getTimeStamp(String timeStr, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = simpleDateFormat.parse(timeStr);
            long timeStamp = date.getTime();
            return timeStamp;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
